package com.enfry.common.util.excel;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class SheetStruct extends Cell {

	private static final long serialVersionUID = -277822491440507836L;

	private Table title;
	private Table body;
	private String id;
	private String sheetName;
	private String processClass;
	private long maxRowCount = -1L;

	public long getMaxRowCount() {
		return maxRowCount;
	}

	public void setMaxRowCount(long maxRowCount) {
		this.maxRowCount = maxRowCount;
	}

	public Table getTitle() {
		return title;
	}

	public void setTitle(Table title) {
		this.title = title;
	}

	public Table getBody() {
		return body;
	}

	public void setBody(Table body) {
		this.body = body;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public String getProcessClass() {
		return processClass;
	}

	public void setProcessClass(String processClass) {
		this.processClass = processClass;
	}

	@Override
	public int getWidth() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getHight() {
		// TODO Auto-generated method stub
		return 0;
	}

	public static SheetStruct load(Element element) throws Exception {
		SheetStruct struct = new SheetStruct();
		int x = 0;
		int y = 0;
		try {
			x = Integer.parseInt(element.getAttributeValue("x"));
		} catch (Exception e) {
		}
		struct.setX(x);
		try {
			y = Integer.parseInt(element.getAttributeValue("y"));
		} catch (Exception e) {
		}
		struct.setY(y);
		struct.setId(element.getAttributeValue("id"));
		struct.setSheetName(element.getAttributeValue("name"));
		struct.setProcessClass(element.getAttributeValue("processClass"));

		if ((element.getAttributeValue("maxRow") != null)
				&& (element.getAttributeValue("maxRow").length() > 0)) {
			struct.setMaxRowCount(Long.parseLong(element
					.getAttributeValue("maxRow")));
		}
		Element te = element.getChild("title");
		if (te != null) {
			struct.setTitle(Table.load(te));
		}
		Element be = element.getChild("body");
		if (be != null) {
			struct.setBody(Table.load(be));
		}
		return struct;
	}

	public static SheetStruct load(String fullFileName, String sheetID)
			throws Exception {
		InputStream is = null;
		try {
			is = new FileInputStream(fullFileName);
			SheetStruct localSheetStruct = load(is, sheetID);
			return localSheetStruct;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (Exception e) {

				}
			}
		}
	}

	public static SheetStruct load(InputStream is, String sheetID)
			throws Exception {
		SAXBuilder sb = new SAXBuilder();
		Document doc = sb.build(is);
		Element root = doc.getRootElement();
		List list = root.getChildren("sheet");
		for (int i = 0; i < list.size(); i++) {
			Element e = (Element) list.get(i);
			if (sheetID.equalsIgnoreCase(e.getAttributeValue("id"))) {
				return load(e);
			}
		}
		return null;
	}

}
